<template>
  <el-dialog
    :title="!dataForm.groupId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="新房名称" prop="flatsName">
      <el-input v-model="dataForm.flatsName" placeholder="新房名称"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="startTime">
      <el-input v-model="dataForm.startTime" placeholder="开始时间"></el-input>
    </el-form-item>
    <el-form-item label="结束时间" prop="endTime">
      <el-input v-model="dataForm.endTime" placeholder="结束时间"></el-input>
    </el-form-item>
    <el-form-item label="团购人数" prop="peoCont">
      <el-input v-model="dataForm.peoCont" placeholder="团购人数"></el-input>
    </el-form-item>
    <el-form-item label="开团人" prop="openGroupPeo">
      <el-input v-model="dataForm.openGroupPeo" placeholder="开团人"></el-input>
    </el-form-item>
    <el-form-item label="开团人id" prop="openGroupPeoId">
      <el-input v-model="dataForm.openGroupPeoId" placeholder="开团人id"></el-input>
    </el-form-item>
    <el-form-item label="新房id" prop="flatsId">
      <el-input v-model="dataForm.flatsId" placeholder="新房id"></el-input>
    </el-form-item>
    <el-form-item label="最低开团人数" prop="minPeoCont">
      <el-input v-model="dataForm.minPeoCont" placeholder="最低开团人数"></el-input>
    </el-form-item>
    <el-form-item label="后悔期限 单位：每天 24小时" prop="regretTime">
      <el-input v-model="dataForm.regretTime" placeholder="后悔期限 单位：每天 24小时"></el-input>
    </el-form-item>
    <el-form-item label="违约金" prop="regretPay">
      <el-input v-model="dataForm.regretPay" placeholder="违约金"></el-input>
    </el-form-item>
    <el-form-item label="团购抽取比例 买房省下的乘比例" prop="extactionRate">
      <el-input v-model="dataForm.extactionRate" placeholder="团购抽取比例 买房省下的乘比例"></el-input>
    </el-form-item>
    <el-form-item label="其他优惠" prop="groupDesc">
      <el-input v-model="dataForm.groupDesc" placeholder="其他优惠"></el-input>
    </el-form-item>
    <el-form-item label="最大团购人数" prop="maxPeople">
      <el-input v-model="dataForm.maxPeople" placeholder="最大团购人数"></el-input>
    </el-form-item>
    <el-form-item label="团购状态 0.未开团 1.团购中 2.成团 3.失败" prop="groupStatus">
      <el-input v-model="dataForm.groupStatus" placeholder="团购状态 0.未开团 1.团购中 2.成团 3.失败"></el-input>
    </el-form-item>
    <el-form-item label="支付时间" prop="deliveryTime">
      <el-input v-model="dataForm.deliveryTime" placeholder="支付时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          groupId: 0,
          flatsName: '',
          startTime: '',
          endTime: '',
          peoCont: '',
          openGroupPeo: '',
          openGroupPeoId: '',
          flatsId: '',
          minPeoCont: '',
          regretTime: '',
          regretPay: '',
          extactionRate: '',
          groupDesc: '',
          maxPeople: '',
          groupStatus: '',
          deliveryTime: ''
        },
        dataRule: {
          flatsName: [
            { required: true, message: '新房名称不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          peoCont: [
            { required: true, message: '团购人数不能为空', trigger: 'blur' }
          ],
          openGroupPeo: [
            { required: true, message: '开团人不能为空', trigger: 'blur' }
          ],
          openGroupPeoId: [
            { required: true, message: '开团人id不能为空', trigger: 'blur' }
          ],
          flatsId: [
            { required: true, message: '新房id不能为空', trigger: 'blur' }
          ],
          minPeoCont: [
            { required: true, message: '最低开团人数不能为空', trigger: 'blur' }
          ],
          regretTime: [
            { required: true, message: '后悔期限 单位：每天 24小时不能为空', trigger: 'blur' }
          ],
          regretPay: [
            { required: true, message: '违约金不能为空', trigger: 'blur' }
          ],
          extactionRate: [
            { required: true, message: '团购抽取比例 买房省下的乘比例不能为空', trigger: 'blur' }
          ],
          groupDesc: [
            { required: true, message: '其他优惠不能为空', trigger: 'blur' }
          ],
          maxPeople: [
            { required: true, message: '最大团购人数不能为空', trigger: 'blur' }
          ],
          groupStatus: [
            { required: true, message: '团购状态 0.未开团 1.团购中 2.成团 3.失败不能为空', trigger: 'blur' }
          ],
          deliveryTime: [
            { required: true, message: '支付时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.groupId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.groupId) {
            this.$http({
              url: this.$http.adornUrl(`/tgroup/group/info/${this.dataForm.groupId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.flatsName = data.group.flatsName
                this.dataForm.startTime = data.group.startTime
                this.dataForm.endTime = data.group.endTime
                this.dataForm.peoCont = data.group.peoCont
                this.dataForm.openGroupPeo = data.group.openGroupPeo
                this.dataForm.openGroupPeoId = data.group.openGroupPeoId
                this.dataForm.flatsId = data.group.flatsId
                this.dataForm.minPeoCont = data.group.minPeoCont
                this.dataForm.regretTime = data.group.regretTime
                this.dataForm.regretPay = data.group.regretPay
                this.dataForm.extactionRate = data.group.extactionRate
                this.dataForm.groupDesc = data.group.groupDesc
                this.dataForm.maxPeople = data.group.maxPeople
                this.dataForm.groupStatus = data.group.groupStatus
                this.dataForm.deliveryTime = data.group.deliveryTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/tgroup/group/${!this.dataForm.groupId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'groupId': this.dataForm.groupId || undefined,
                'flatsName': this.dataForm.flatsName,
                'startTime': this.dataForm.startTime,
                'endTime': this.dataForm.endTime,
                'peoCont': this.dataForm.peoCont,
                'openGroupPeo': this.dataForm.openGroupPeo,
                'openGroupPeoId': this.dataForm.openGroupPeoId,
                'flatsId': this.dataForm.flatsId,
                'minPeoCont': this.dataForm.minPeoCont,
                'regretTime': this.dataForm.regretTime,
                'regretPay': this.dataForm.regretPay,
                'extactionRate': this.dataForm.extactionRate,
                'groupDesc': this.dataForm.groupDesc,
                'maxPeople': this.dataForm.maxPeople,
                'groupStatus': this.dataForm.groupStatus,
                'deliveryTime': this.dataForm.deliveryTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
